/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aaa;

import java.awt.Image;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.*;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.filechooser.FileSystemView;

/**
 *
 * @author kurpicz
 */
public class Configurator extends javax.swing.JFrame  {

    /**
     * Creates new form Configurator
     */
    final JFrame conf;
    
    public Configurator() throws IOException {
        initComponents();
        this.setTitle("Configuration Settings");
        Image im = ImageIO.read(this.getClass().getResource("/resources/HP-Control.png"));
        this.setIconImage(im);
        textApkToolPath.setText(getConfigProperty("apk_output_path"));
        textExcelPath.setText(getConfigProperty("xls_path"));
        conf = this;
        this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);        
        this.addWindowListener(new WindowListener() {

            public void windowClosed(WindowEvent arg0) {
                System.out.println("Window close event occur");
            }
            public void windowActivated(WindowEvent arg0) {
                System.out.println("Window Activated");
            }
            public void windowClosing(WindowEvent arg0) {
                conf.setVisible(false);
            }
            public void windowDeactivated(WindowEvent arg0) {
                System.out.println("Window Deactivated");
            }
            public void windowDeiconified(WindowEvent arg0) {
                System.out.println("Window Deiconified");
            }
            public void windowIconified(WindowEvent arg0) {
                System.out.println("Window Iconified");
            }
            public void windowOpened(WindowEvent arg0) {
                System.out.println("Window Opened");
            }
        });
    }
    
    public static String getConfigProperty(String propName) throws FileNotFoundException, IOException{
        Properties prop = new Properties();
        FileInputStream fis;
        try {
            fis = new FileInputStream("ConfigAAA.xml");
            prop.loadFromXML(fis);
            return prop.getProperty(propName, "");
        } catch (IOException ex) {
            return "";
        }     
    }

    public static String getPath(){   
        return "";
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblExcelPath = new javax.swing.JLabel();
        textExcelPath = new javax.swing.JTextField();
        btnSave = new javax.swing.JButton();
        btnChooseExcelPath = new javax.swing.JButton();
        lblApkToolPath = new javax.swing.JLabel();
        textApkToolPath = new javax.swing.JTextField();
        btnChooseApkToolPath = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        lblExcelPath.setText("Excel Output Path:");

        textExcelPath.setText(Configurator.getPath());

        btnSave.setText("Save");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        btnChooseExcelPath.setText("...");
        btnChooseExcelPath.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnChooseExcelPathActionPerformed(evt);
            }
        });

        lblApkToolPath.setText("Apk Output Path:");

        textApkToolPath.setText(Configurator.getPath());

        btnChooseApkToolPath.setText("...");
        btnChooseApkToolPath.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnChooseApkToolPathActionPerformed(evt);
            }
        });

        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(19, 19, 19)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createSequentialGroup()
                                .add(lblExcelPath, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 110, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 17, Short.MAX_VALUE)
                                .add(textExcelPath, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 321, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(btnChooseExcelPath, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 22, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                                .add(lblApkToolPath, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .add(textApkToolPath, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 321, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(btnChooseApkToolPath, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 22, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(btnSave)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(btnCancel)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(20, 20, 20)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lblExcelPath)
                    .add(textExcelPath, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(btnChooseExcelPath))
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lblApkToolPath)
                    .add(textApkToolPath, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(btnChooseApkToolPath))
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(btnSave)
                    .add(btnCancel))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    // This is the save button
    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        Properties prop = new Properties();
        prop.setProperty("xls_path", textExcelPath.getText());
        prop.setProperty("apk_output_path", textApkToolPath.getText());
        try {
            FileOutputStream fos = new FileOutputStream("ConfigAAA.xml");
            try {
                prop.storeToXML(fos, "AAA Configuration", "UTF-8");
            } catch (IOException ex) {
                Logger.getLogger(Configurator.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Configurator.class.getName()).log(Level.SEVERE, null, ex);
        }
        this.setVisible(false);
    }//GEN-LAST:event_btnSaveActionPerformed

    private void btnChooseExcelPathActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnChooseExcelPathActionPerformed
        FileSystemView filesys = FileSystemView.getFileSystemView();
        String desktopPath = filesys.getHomeDirectory().getPath();
        JFileChooser fileChooserForm = new JFileChooser(desktopPath);
        fileChooserForm.setVisible(rootPaneCheckingEnabled);
        fileChooserForm.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int retVal = fileChooserForm.showOpenDialog(this);
        if (retVal == JFileChooser.APPROVE_OPTION) {
            textExcelPath.setText(fileChooserForm.getSelectedFile().getPath());
        }

    }//GEN-LAST:event_btnChooseExcelPathActionPerformed

    private void btnChooseApkToolPathActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnChooseApkToolPathActionPerformed
        FileSystemView filesys = FileSystemView.getFileSystemView();
        String desktopPath = filesys.getHomeDirectory().getPath();
        JFileChooser fileChooserForm = new JFileChooser(desktopPath);
        fileChooserForm.setVisible(rootPaneCheckingEnabled);
        fileChooserForm.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int retVal = fileChooserForm.showOpenDialog(this);
        if (retVal == JFileChooser.APPROVE_OPTION) {
            textApkToolPath.setText(fileChooserForm.getSelectedFile().getPath());
        }
    }//GEN-LAST:event_btnChooseApkToolPathActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_btnCancelActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Configurator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Configurator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Configurator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Configurator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run()  {
                try {
                new Configurator().setVisible(true);
                }
                catch (IOException ex1) { }
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnChooseApkToolPath;
    private javax.swing.JButton btnChooseExcelPath;
    private javax.swing.JButton btnSave;
    private javax.swing.JLabel lblApkToolPath;
    private javax.swing.JLabel lblExcelPath;
    private javax.swing.JTextField textApkToolPath;
    private javax.swing.JTextField textExcelPath;
    // End of variables declaration//GEN-END:variables
}
